package com.itheima.up_web.controller;

import cn.itcast.up.common.HDFSUtils;
import cn.itcast.up.common.HdfsTools;
import com.alibaba.fastjson.JSON;
import com.itheima.up_web.bean.dto.ModelDto;
import com.itheima.up_web.bean.dto.TagDto;
import com.itheima.up_web.bean.dto.TagModel;
import com.itheima.up_web.bean.http.HttpResult;
import com.itheima.up_web.service.TagService;
import com.itheima.up_web.util.Codes;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * 添加123级标签.
     * [{"label":"一级标签名称","name":"电商","level":"1"},
     * {"label":"二级标签名称","name":"京东商城","level":"2"},
     * {"label":"三级标签名称","name":"人口属性","level":"3"}]
     * @RequestBody 将请求的Json转换为对象
     */

    @PutMapping("tags/relation") //限制put请求方式
    public void addTags(@RequestBody List<TagDto> list){
        //将list保存到数据库,将list交给service进行处理
        tagService.saveAll(list);
    }

    //查询123级标签列表
    @GetMapping("tags")
    public HttpResult<List<TagDto>> findTags(Long pid){
        List<TagDto> list = tagService.findTagsByPid(pid);
        return new HttpResult<List<TagDto>>(Codes.SUCCESS,"成功",list);
    }

    //获取4级标签创建代码
    @PutMapping("tags/model")
    public void saveTagModel(@RequestBody TagModel tagModel){
        //获取对应的标签和模型
        tagService.saveTagModel(tagModel.getTag(),tagModel.getModel());
    }

    //获取4级标签列表
    @GetMapping("tags/model")
    public HttpResult getTagModel(Long pid){
        //根据3级标签的pid获取4级标签和模型
        List<TagModel> list = null;
        list = tagService.findAllTagModel(pid);

        HttpResult<List<TagModel>> result = new HttpResult<>(Codes.SUCCESS, "查询成功", list);
        String json = JSON.toJSONString(result);
        System.out.println(json);
        return result;
    }

    //创建5级标签
    @PutMapping("tags/data")
    public HttpResult putFiveTag(@RequestBody TagDto dto){
        tagService.saveTag(dto);
        return new HttpResult(Codes.SUCCESS,"5级标签创建成功", dto);
    }

    /**
     * 文件上传
     * @param file MultipartFile 负责接收请求携带的文件 ,这里必须叫file
     * @return
     */
    @PostMapping("tags/upload")
    public HttpResult uploadFile(MultipartFile file) throws IOException {
        //文件的minitype类型
        String type = file.getContentType();
        //上传的参数名字
        String name = file.getName();
        //上传的文件名字
        String originalFilename = file.getOriginalFilename();
        //上传文件的大小
        long size = file.getSize();
        //System.out.println("type:" + type);
        //System.out.println("name:" + name);
        //System.out.println("size:" + size);
        //System.out.println("originalFilename:" + originalFilename);

        InputStream inputStream = file.getInputStream();
        //文件保存到本地,临时文件
        IOUtils.copy(inputStream, new FileOutputStream(new File("a.jar")));

        //上传文件到HDFS上
        //定义文件名
        String fileName = UUID.randomUUID().toString() + ".jar";
        String path = "/tmp/" + fileName;
        System.out.println(path);
        HdfsTools.build().uploadLocalFile2HDFS("a.jar",path);
//        HDFSUtils.getInstance().copyFromFile("a.jar",path);
        return new HttpResult(Codes.SUCCESS,"success","hdfs://node01:9000" + path);
    }

    //启动/停止模型
    @PostMapping("tags/{id}/model")
    public HttpResult changeModelState(@PathVariable Long id, @RequestBody ModelDto modelDto){
        tagService.updateModelState(id,modelDto.getState());
        return new HttpResult(Codes.SUCCESS,"执行成功",null);
    }
}
